/**
 * 
 */
package com.sfeir.client.activity;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.Image;
import com.google.web.bindery.event.shared.EventBus;
import com.sfeir.canvas.activities.client.AbstractCanvasActivity;
import com.sfeir.canvas.activities.client.event.PageChangeEvent;
import com.sfeir.canvas.util.client.ResourceManager;
import com.sfeir.client.i18n.TetrisConstants;
import com.sfeir.client.util.Constants;

/**
 * @author François LAROCHE
 * 
 */
public class HowToActivity extends AbstractCanvasActivity {

	private static final int BACK_BUTTON_X = 100;
	private static final int BACK_BUTTON_Y = 450;
	private static final int BACK_BUTTON_SIZE = 64;

	private final Image panel;
	private final Image back;
	private final Image background;
	private final TetrisConstants texts;
	private final Image key;
	private final Image space;
	private EventBus bus;

	public HowToActivity(ResourceManager manager) {
		this.panel = manager.getImage(Constants.KEY_IMAGE_PANEL);
		this.back = manager.getImage(Constants.KEY_IMAGE_BACK);
		this.background = manager.getImage(Constants.KEY_IMAGE_BACKGROUND);
		this.key = manager.getImage(Constants.KEY_IMAGE_KEY_BLANK);
		this.space = manager.getImage(Constants.KEY_IMAGE_KEY_SPACE);
		this.texts = GWT.create(TetrisConstants.class);
	}

	@Override
	public void start(Canvas canvas, EventBus bus) {
		this.bus = bus;
		Context2d ctx = canvas.getContext2d();
		ctx.drawImage(ImageElement.as(background.getElement()), 0, 0);
		ctx.drawImage(ImageElement.as(panel.getElement()), 0, 0);
		ctx.drawImage(ImageElement.as(back.getElement()), BACK_BUTTON_X, BACK_BUTTON_Y);
		ctx.save();

		ctx.setFont("bold 30pt calibri");
		ctx.fillText(texts.howTo(), 300, 120);

		ctx.setFont("16pt calibri");
		ctx.fillText(texts.press(), 150, 240);
		ctx.drawImage(ImageElement.as(this.key.getElement()), 240, 213);
		ctx.fillText("a", 251, 238);
		ctx.fillText(texts.or(), 290, 240);
		ctx.drawImage(ImageElement.as(this.key.getElement()), 330, 213);
		ctx.fillText("z", 342, 238);
		ctx.fillText(texts.rotate(), 380, 240);
		
		ctx.fillText(texts.press(), 150, 295);
		ctx.drawImage(ImageElement.as(this.space.getElement()), 240, 273);
		ctx.fillText(texts.drop(), 450, 295);
		
		ctx.fillText(texts.move(), 150, 350);
		
		ctx.fillText(texts.goal(), 150, 400);
		ctx.fillText(texts.scoring(), 150, 450);

		ctx.restore();
	}

	@Override
	public void onClick(ClickEvent event) {
		int x = event.getX();
		int y = event.getY();
		if (x >= BACK_BUTTON_X && x <= BACK_BUTTON_X + BACK_BUTTON_SIZE && y >= BACK_BUTTON_Y
				&& y <= BACK_BUTTON_Y + BACK_BUTTON_SIZE) {
			this.bus.fireEvent(new PageChangeEvent(PageChangeEvent.BACK, null));
		}
	}
}
